from keystoneauth1.identity import v3
from keystoneauth1 import session
from glanceclient import Client


# auth_url为keystone的endpoint入口
def get_keystone_session():
    auth = v3.Password(auth_url="http://192.168.10.10:5000/v3",
                       username="admin", password="000000",
                       project_name="admin", user_domain_name="demo",
                       project_domain_name="demo")
    sess = session.Session(auth=auth)
    return sess


# 调用glance
def get_glance_client():
    sess = get_keystone_session()
    glance = Client('2', session=sess)
    return glance


# 查询镜像列表
def list_image():
    list = glance.images.list()
    for image in list:
        print(image.name, image.id, image.status)


if __name__ == "__main__":
    glance = get_glance_client()
    # 根据对应ID和镜像文件构建一个active的镜像
    id = 'b2606f30-6f45-4551-944e-b23bf2ed0cec'
    glance.images.upload(id, open('/root/CentOS_7.2_x86_64_XD.qcow2', 'rb'))
    print('#######   list after upload qcow2   #########')
    list_image()
